<?

/**
* Global processing functions
*
* @author Adesanto Asman
* @version 1.0
* @license Free
* @package GlobalProcessors
*
*/

class GlobalProcessors{

  /**
   * drop one or more entry of array
   *
   * @param array   $lstArray
   * @param array   $lstKey
   * @param array   $nDrop    n drop
   */
  function dropArray(&$lstArray,
                      $lstKey,
                      $dropBox){

    /**
    * splice the dummy
    * The first 2 params are to shrink the array
    * e.g: array_splice($dummy, 2); is to shrink dummy to 2 elements
    *
    * The last 2 params is to concanate the array
    * it tells how many array to concat and the content of the array
    *
    * dropPoint needs to be reduce by 1 so we could have the cursor
    * of array for $dummy at the correct posisition
    * e.g: dropPoint = 3, meaning the 3rd elements is going to be dust off
    *      so we only save the first and 2nd element. Then use splice
    *      to concat the 4th...nElement with the first 2 element.
    *
    */

    /**
    * get how many selected drops
    *
    */
    $dropTimes = sizeof($dropBox);

    foreach($lstKey as $list){

      for($i = $dropTimes ; $i > 0 ; $i--){

        /**
        * Since $_POST["dropBox"] index is using array numbering
        * then we need to reduce the droppoint by 1.
        * E.g: $dropTimes = 3 => $_POST["dropBox"][3] will result Error
        *      since the dropTimes is trying to point to 3rd element
        *      of $_POST["dropBox"], which array wise is at [2] meaning 3rd element
        *
        */
        $dropPoint = $dropBox[($i-1)];

        /**
        * slice it! so we could have the buffer's tail
        *
        */
        $bufTail   = array_slice($lstArray[$list], $dropPoint);

        //replace it
        array_splice($lstArray[$list],             /* The original array or dummy */
                     ($dropPoint - ARRAY_NUMBER),  /* Calculate the cursor, since it comes with array numbering */
                     sizeof($bufTail),             /* The sizeof array concat with */
                     $bufTail);                    /* The array we concat with */

        /**
         * throw the last mem bank
         */
        array_pop($lstArray[$list]);
      }
    }
    unset($list);
  }

  /**
   * @author Anton "The MissSpell"
   *
   * Calculator of days difference
   *
   * @param  date  $endDate
   * @param  date  $beginDate
   */
  function daysDifference($endDate, $beginDate){

    define("_DATE_DAY",           0);
    define("_DATE_MONTH",         1);
    define("_DATE_YEAR",          2);

    /**
    * Extract the date string
    * explode the date by "-" and storing to array
    *
    */
    $beginDate  = explode("-", $beginDate);
    $endDate    = explode("-", $endDate);

    /**
    * gregoriantojd() Converts a Gregorian date to Julian Day Count
    *
    */
    $startDate  = date("d-m-Y",
                       mktime(0,0,0, $beginDate[_DATE_MONTH],
                                     $beginDate[_DATE_DAY],
                                     $beginDate[_DATE_YEAR]));
    $stopDate   = date("d-m-Y",
                       mktime(0,0,0, $endDate[_DATE_MONTH],
                                     $endDate[_DATE_DAY],
                                     $endDate[_DATE_YEAR]));

    return $stopDate - $startDate;
  }

  /**
   * get temporary JID
   *
   * @return string
   */
  function getTmpJid(){

    srand();

    return date("his") . rand();
  }

  /**
   * check the SN into DB
   * Make sure that there is no duplicate
   *
   * @param Misc      $Misc
   * @param integer   $qty
   * @param integer   $strStart
   * @param integer   $strLength
   * @param string    $srcSN
   * @param Database  $Db
   * @param string    $srcDb
   * @return string on success return booelan FALSE, fail return the serial number
   */
  function isSnValid($Misc,
                     $qty,
                     $strStart,
                     $strLength,
                     $srcSN,
                     $Db,
                     $srcDb){

    /**
     * the number length
     * eg. BC010 with head: 2 and total length 5
     *     the number length would be 5-2 = 3
     *     thus we have BC : 2 and 010 : 3
     */
    $strLength -= $strStart;

    /**
     * extract the SN
     */
    $SN = explode("-", $srcSN);

    $theSerial = substr($SN[_SN_HEAD],
                        _DEFAULT_START,
                        $strStart);

    $theNumber = substr($SN[_SN_HEAD],
                        $strStart,
                        $strLength);

    for($iNum = 0; $iNum < $qty; $iNum++){

      $tmpNum = (int)$theNumber + (int)$iNum;

      $chkSn = $theSerial.
               $Misc->addDigit2($strLength,
                                (string)$tmpNum).
               $tmpNum;

      GlobalFetchers::fetchIsSnValid($Db,
                                     $chkSn,
                                     $srcDb,
                                     $isValid);

      if($isValid == 1){

        /* Continue the process/Loop */
      }else{

        return $chkSn;
      }
    }

    return false;
  }

  /**
  * First Menu login
  *
  * @param  string    $message      user message
  *
  */
  function procLogin(&$message){

    /**
    * If we have flash message set
    * meaning the login has failed or
    * something happened
    *
    */
    if($message){

      $theLabel = $message;
    }else{

      $theLabel = "Login";
    }

    echo  "<p align='center'><br/><br/><br/><br/><b>.:".$theLabel.":.</b></p>".
        	"<form action=# method=post>".
          " <table border=1 align='center' class='dedeq'>".
          "  	<tr >".
          "		<td ><p>ID   </td><td ><input type=text     name='user' size='20' maxlength='20'> </p></td></tr>".
          "		<tr >".
          "		<td ><p>Pass </td><td ><input type=password name='pass' size='20' maxlength='20'> </p></td></tr>".
          "		</center>".
          "	</table>".
          "	<p align='center'><input type='submit' value='Login' name='continue' align='center'> </p>".
        	"</form>".
        	"</p>";
  }

  /**
  * Re validation form
  *
  * @param  string  $message        user message
  * @param  string  $tgtUrl         target urldecode
  * @param  string  $btnLabel       button label
  * @param  string  $btnName        button name
  * @param  string  $btnValue       button value
  * @param  string  $btnProperties  button properties
  *
  */
  function procChkValidation(&$message,
                             &$tgtUrl,
                             &$btnLabel,
                             &$btnName,
                             &$btnValue,
                             &$btnProperties){

    echo  "<br/><br/><br/><br/>\n".

  	      "<p align='center'><b>.:".$message.":.</b></p>\n".

  	      "<form action=".$tgtUrl." method=post>\n".
  		    "<table border=1 align='center' class='dedeq'>\n".
  		    "<tr>\n".

  				  "<td><p>ID      </td><td><input type=text     name='reauth_user' size='20'> </p></td>\n".

  				"</tr>\n".
  				"<tr>\n".

  					"<td><p>Pass    </td><td><input type=password name='reauth_pass' size='20'> </p></td>\n".

  				"</tr>\n".
  				"</table>\n".

  			  "<p align='center'><button name='".$btnName."' value='".$btnValue."' type='submit' ".$btnProperties.">".$btnLabel."</button></p>\n".
  				"</form>";

  }

  function formOkValidated(&$message,
  				                 &$tgtUrl,
  				                 &$btnLabel,
  				                 &$btnName,
  				                 &$btnValue,
  				                 &$btnProperties){

    echo  "<br/><br/><br/><br/><br/><br/>\n".

  			  "<p align='center'><b>.:".$message.":.</b></p>\n".

  				"<form action=".$tgtUrl." method=POST></p>\n".

  				"<p align='center'><button name='".$btnName."' value='".$btnValue."' type='submit' ".$btnProperties.">".$btnLabel."</button></p>\n".
  				"</form> ";
  }

  /**
  * Create custom serialization
  *
  * @param  string    $userId
  * @param  string    $srcValues
  *
  */
  function customSerialize(&$userId, &$srcValues){

    $srcDbHdr = "USER_TEMP_VARS_HDR";
    $srcDbDtl = "USER_TEMP_VARS_DTL";

    $Db = new Oracle;

    $Db->init($_SESSION["ORCL"],
              $_SESSION["OPASS"],
              $_SESSION["SID"]);

    if($Db->connect()){

      GlobalHandlers::initSerialize($COracle,
                                    $userId,
                                    $srcValues,
                                    $srcDbHdr,
                                    $srcDbDtl);

    }else{

      Misc::usrDie("Error: Db connection failed, serializing failed");

    }
  }

  /**
  * Create custom serialization
  *
  * @param  string    $userId
  * @param  string    $srcValues
  * @param  string    $theResult
  *
  */
  function customUnserialize(&$userId,
                             &$srcValues,
                             &$theResult){

    $srcDbHdr = "USER_TEMP_VARS_HDR";
    $srcDbDtl = "USER_TEMP_VARS_DTL";

    $Db = new Oracle;

    $Db->init($_SESSION["ORCL"],
              $_SESSION["OPASS"],
              $_SESSION["SID"]);

    if($Db->connect()){

      GlobalFetchers::fetchUnserialize($COracle,
                                       $userId,
                                       $srcValues,
                                       $srcDbHdr,
                                       $srcDbDtl,
                                       $theResult);

    }else{

      Misc::usrDie("Error: Db connection failed, serializing failed");

    }
  }

  /**
  * init all additional variables
  *
  * @param void
  *
  */
  function procVarsAdditional(){

    $srcDbMstLevelD = "MST_LEVELD";

    $Db = new Oracle;

    $Db->init($_SESSION["ORCL"],
              $_SESSION["OPASS"],
              $_SESSION["SID"],
              _NO_COMMENT);

    if($Db->connect()){

    	GlobalFetchers::fetchSelLevelD($Db,
                                     $srcDbMstLevelD,
                                     $thisLevelD);

      $_SESSION["curLevelD"] = $thisLevelD;

    	$_SESSION["sysPeriode"]= date("m-Y");

      $Db->dc();
    }else{

      Misc::usrErr("Db Connection Failed");
    }
  }
}

?>